package br.edu.unilasalle.simon_api.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.edu.unilasalle.simon_api.dao.IGenericDao;
import br.edu.unilasalle.simon_api.domain.BaseEntity;
import br.edu.unilasalle.simon_api.service.IGenericService;

@Service("genericService")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public abstract class GenericService<T extends BaseEntity<ID>, ID extends Serializable> implements IGenericService<T, ID> {

	public abstract IGenericDao<T, ID> getEntityDao();

	public T find(T entity) {
		return findById(entity.getId());
	}

	public T findById(ID id) {
		return getEntityDao().findById(id);
	}
	
	public List<T> findAll() {
		return getEntityDao().findAll();
	}

	@Transactional(readOnly = false)
	public T save(T entity) {
		return getEntityDao().save(entity);	
	}
	
	@Transactional(readOnly = false)
	public T merge(T entity){
		return getEntityDao().merge(entity);
	}
	
	@Transactional(readOnly = false)
	public void delete(T entity) {
		getEntityDao().delete(entity);
	}
	
	@Transactional(readOnly = false)
	public void deleteById(ID id) {
		T entity = findById(id);
		delete(entity);
	}

	public void refresh(T entity){
		getEntityDao().refresh(entity);
	}
	
	public T getReference(ID id){
		return getEntityDao().getReference(id);
	}

}